import * as TYPES from './actionTypes';
import {AllOrderUrl,UserOrderUrl,CancelOrderUrl} from '../../Component/common/url';

// ORDERALL
export function OrderAll(token){
	return (dispatch) => {
		let order_all = fetch(AllOrderUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					"Authorization": "Bearer "+token
				},
				body:JSON.stringify({state:["finish","canceling","going","cancel"]})
			})
			.then(response=>response.json())
			.then((data)=>{
				dispatch({'type': TYPES.ORDER_ALL, order: data});
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}
// ORDERGOING
export function OrderGoing(token){
	return (dispatch) => {
		let order_all = fetch(AllOrderUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					"Authorization": "Bearer "+token
				},
				body:JSON.stringify({state:["going"]})
			})
			.then(response=>response.json())
			.then((data)=>{
				dispatch({'type': TYPES.ORDER_ALL, order: data});
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}
// ORDERFINISH
export function OrderFinish(token){
	return (dispatch) => {
		let order_all = fetch(AllOrderUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					"Authorization": "Bearer "+token
				},
				body:JSON.stringify({state:["finish"]})
			})
			.then(response=>response.json())
			.then((data)=>{
				dispatch({'type': TYPES.ORDER_ALL, order: data});
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}
// ORDERCANCELING
export function OrderCanceling(token){
	return (dispatch) => {
		let order_all = fetch(AllOrderUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					"Authorization": "Bearer "+token
				},
				body:JSON.stringify({state:["canceling"]})
			})
			.then(response=>response.json())
			.then((data)=>{
				dispatch({'type': TYPES.ORDER_ALL, order: data});
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}
// ORDERCANCEL
export function OrderCancel(token){
	return (dispatch) => {
		let order_all = fetch(AllOrderUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					"Authorization": "Bearer "+token
				},
				body:JSON.stringify({state:["cancel"]})
			})
			.then(response=>response.json())
			.then((data)=>{
				dispatch({'type': TYPES.ORDER_ALL, order: data});
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}

//ORDER_USER
export function OrderUser(token,opt){
	return (dispatch) => {
		let order_all = fetch(UserOrderUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					"Authorization": "Bearer "+token
				},
				body:JSON.stringify(opt)
			})
			.then(response=>response.json())
			.then((data)=>{
				dispatch({'type': TYPES.ORDER_USER, orderuser: data});
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}
//TO_CANCEL_ORDER
export function ToCancelOrder(token,opt){
	return (dispatch) => {
		let order_all = fetch(CancelOrderUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					"Authorization": "Bearer "+token
				},
				body:JSON.stringify(opt)
			})
			.then(response=>response.json())
			.then((data)=>{
				dispatch({'type': TYPES.ORDER_USER, orderuser: data});
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}

